﻿using System;
using SmartSiteManagementSystem.IRepository.WorkTask;
using SmartSiteManagementSystem.Models.WorkTask;
using SmartSiteManagementSystem.Repository.DBHelper;
using System.Data;
using Dapper;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using MySql.Data.MySqlClient;

namespace SmartSiteManagementSystem.Repository.WorkTask
{
    public class SupplyingPlaceInfoRepository : ISupplyingPlaceInfoRepository
    {
        #region dataTable
        /*
drop table if exists `supplying_place`;
create table `supplying_place`( 
	`supplying_place_id` int(11) not null AUTO_INCREMENT primary key, 
	`supplying_place_name` nvarchar(250) NOT NULL,
    `position_x` double(10,2)  NOT NULL default 0,
	`position_y` double(10,2)  NOT NULL default 0,
	`position_z` double(10,2)  NOT NULL default 0,
    `descript` text null,
    `is_delete` bit NOT NULL default 0,
	`create_time` datetime not null default now(),
	`update_time` datetime not null default now()
) default charset=utf8;
         */
        #endregion
        //private IDbConnection _readConn;
       // private IDbConnection _writeConn;

        public bool Delete(Hashtable param)
        {
            bool back = false;
            if (param == null)
                param = new Hashtable();
            String querySql = @"delete from supplying_place";
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where ");
                    int parCount = 0;
                    if (param.ContainsKey("supplying_place_id"))
                    {
                        pars.Add("supplying_place_id", param["supplying_place_id"]);

                        whereCondition.Append(" supplying_place_id =@supplying_place_id");
                        parCount++;
                    }
                }
                string execSql = querySql + whereCondition.ToString();
                int backInt = _writeConn.Execute(execSql, pars);
                if (backInt > 0)
                {
                    back = true;
                }
            }
            return back;
        }

        public SupplyingPlaceInfo Get(Hashtable param)
        {
            SupplyingPlaceInfo back = null;
            if (param == null)
                param = new Hashtable();
            String querySql = @"SELECT supplying_place_id as SupplyingPlaceId,supplying_place_name as SupplyingPlaceName,position_x as PositionX,position_y as PositionY,position_z as PositionZ,descript as Descript,is_delete as IsDelete,create_time as CreateTime,update_time as UpdateTime FROM supplying_place";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {

                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("supplying_place_id"))
                    {
                        pars.Add("supplying_place_id", param["supplying_place_id"]);
                        whereCondition.Append(" and supplying_place_id =@supplying_place_id");
                    }
                    if (param.ContainsKey("supplying_place_name"))
                    {
                        pars.Add("supplying_place_name", param["supplying_place_name"]);
                        whereCondition.Append(" and supplying_place_name =@supplying_place_name");
                    }

                }
                whereCondition.Append(" limit 1");
                string execSql = querySql + whereCondition.ToString();
                back = _readConn.QueryFirstOrDefault<SupplyingPlaceInfo>(execSql, pars);
            }
            return back;
        }

        public int GetCount(Hashtable param)
        {
            int back = 0;
            if (param == null)
                param = new Hashtable();
            String querySql = "select count(supplying_place_id) from supplying_place";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("supplying_place_id"))
                    {
                        pars.Add("supplying_place_id", param["supplying_place_id"]);
                        whereCondition.Append(" and supplying_place_id =@supplying_place_id");
                    }
                    if (param.ContainsKey("supplying_place_name"))
                    {
                        pars.Add("supplying_place_name", param["supplying_place_name"]);
                        whereCondition.Append(" and supplying_place_name =@supplying_place_name");
                    }
                    if (param.ContainsKey("is_delete"))
                    {
                        pars.Add("is_delete", param["is_delete"]);
                        whereCondition.Append(" and is_delete =@is_delete");
                    }
                }

                string execSql = querySql + whereCondition.ToString();
                #endregion
                back = _readConn.QueryFirstOrDefault<int>(execSql, pars);
            }
            return back;
        }

        public IList<SupplyingPlaceInfo> GetList(Hashtable param, string orderBy, int first, int rows)
        {

            IList<SupplyingPlaceInfo> back = null;
            if (param == null)
                param = new Hashtable();
            String querySql = @"SELECT supplying_place_id as SupplyingPlaceId,supplying_place_name as SupplyingPlaceName,position_x as PositionX,position_y as PositionY,position_z as PositionZ,descript as Descript,is_delete as IsDelete,create_time as CreateTime,update_time as UpdateTime FROM supplying_place";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("supplying_place_id"))
                    {
                        pars.Add("supplying_place_id", param["supplying_place_id"]);
                        whereCondition.Append(" and supplying_place_id =@supplying_place_id");
                    }
                    if (param.ContainsKey("supplying_place_name"))
                    {
                        pars.Add("supplying_place_name", param["supplying_place_name"]);
                        whereCondition.Append(" and supplying_place_name =@supplying_place_name");
                    }
                    if (param.ContainsKey("is_delete"))
                    {
                        pars.Add("is_delete", param["is_delete"]);
                        whereCondition.Append(" and is_delete =@is_delete");
                    }
                }
                if (string.IsNullOrEmpty(orderBy))
                {
                    whereCondition.Append(" order by supplying_place_id desc");
                }
                else
                {
                    whereCondition.Append(" order by @orderBy");
                    pars.Add("orderBy", orderBy);
                }
                whereCondition.Append(" limit @first,@rows");
                pars.Add("first", first);
                pars.Add("rows", rows);
                #endregion
                string execSql = querySql + whereCondition.ToString();
                back = _readConn.Query<SupplyingPlaceInfo>(execSql, pars).AsList<SupplyingPlaceInfo>();
            }
            return back;
        }

        public int Insert(SupplyingPlaceInfo obj)
        {
            int back = 0;
            #region sql
            string sql = @"INSERT INTO supplying_place
(
supplying_place_name,
position_x,
position_y,
position_z,
descript,
is_delete,
create_time,
update_time
)
VALUES
(
@demanding_place_name,
@position_x,
@position_y,
@position_z,
@descript,
@is_delete,
@create_time,
@update_time);select last_insert_id();";
            #endregion
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                back = _writeConn.QueryFirst<int>(sql, new
                {
                    supplying_place_name = obj.SupplyingPlaceName,
                    position_x=obj.PositionX,
                    @position_y = obj.PositionY,
                    @position_z =obj.PositionZ,
                    is_delete =obj.IsDelete,
                    descript=obj.Descript,
                    create_time = obj.CreateTime,
                    update_time = obj.UpdateTime
                });
            }
            return back;
        }

        public bool Update(SupplyingPlaceInfo obj)
        {
            bool back = false;
            #region sql
            string sql = @"update supplying_place
set
supplying_place_name = @supplying_place_name
position_x=@position_x,
position_y=@position_y,
position_z=@position_z,
descript=@descript,
is_delete =@is_delete ,
update_time =@update_time ,
create_time = @create_time
WHERE supplying_place_id =@supplying_place_id;";
            #endregion
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                int backint = _writeConn.Execute(sql, new
                {
                    supplying_place_id = obj.SupplyingPlaceId,
                    supplying_place_name = obj.SupplyingPlaceName,
                    position_x=obj.PositionX,
                    position_y = obj.PositionY,
                    position_z = obj.PositionZ,
                    descript =obj.Descript,
                    is_delete =obj.IsDelete,
                    create_time = obj.CreateTime,
                    update_time = obj.UpdateTime
                });
                if (backint > 0)
                {
                    back = true;
                }
            }
            return back;
        }
    }
}

